# -*- coding:UTF-8 -*-
from __future__ import print_function
import vad.speech_segmentation as seg
import audio.audio_operator as ff
from speech.ibm.speech_recognition import IBMSpeechRecognizer
from translate.youdao.translate_recognition import YouDaoTranslateRecognizer
import os
import shutil


def video_conversion(path):
    """
    视频转换
        :param path: 文件路径
        :return:
    """
    file_wav = ff.edit_file(path, ctype='wav')
    if not os.path.exists(file_wav):
        ff.change_video(path, 'wav')

    return file_wav


def speech_segmentation(path):
    """
    语音分割
        :param path: 文件路径
        :return:
    """
    frame_size = 256
    frame_shift = 128
    sr = 16000

    # 计算分割点
    seg_point = seg.multi_segmentation(path, sr, frame_size, frame_shift, plot_seg=False)

    # 音频分割输出
    ff.divide_video(path, seg_point)

    return seg_point


def speech_recognition(path):
    """
    语音识别
        :param path: 文件路径
        :return:
    """
    sr = IBMSpeechRecognizer()
    text = sr.wav_file_recognition(path)
    return text


def language_translation(text, o_lan='auto', t_lan='auto'):
    """
    语言翻译
        :param text: 待翻译文本
        :param o_lan: 原始语言
        :param t_lan: 目标语言
        :return:
    """
    tr = YouDaoTranslateRecognizer(lang_from=o_lan, lang_to=t_lan)
    target_text = tr.translate(text)
    return target_text


def subtitle_loading(subtitle_path, file_path):
    """
    字幕加载
        :param subtitle_path: 字幕路径
        :param file_path: 视频路径
        :return:
    """
    ff.load_subtitle(subtitle_path, file_path)


def clean_data(path):
    """
    清除数据
        :param path:  文件/文件夹路径
        :return:
    """
    if os.path.exists(path):
        if os.path.isdir(path):
            if len(os.listdir(path)) == 0:
                os.removedirs(path)
            else:
                shutil.rmtree(path)
        elif os.path.isfile(path):
            os.remove(path)